package com.tsy.server.system.controller;
import com.tsy.server.common.Result;
import com.tsy.server.system.entity.Project;
import com.tsy.server.system.entity.dto.ProjectDTO;
import com.tsy.server.system.entity.vo.ProjectQueryVO;
import com.tsy.server.system.entity.vo.ProjectVO;
import com.tsy.server.system.service.ProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/project")
@Api(tags = "项目管理接口")
public class ProjectController {

    @Autowired
    private ProjectService projectService;

    @GetMapping("/list")
    @ApiOperation("获取项目列表")
    public Result<List<ProjectVO>> listProjects(ProjectQueryVO projectQueryVO) {
        List<Project> project = projectService.listProjects(projectQueryVO);
        return Result.successToVo(project, ProjectVO.class);
    }

    @PostMapping
    @ApiOperation("创建项目")
    @PreAuthorize("hasAuthority('admin')")
    public Result<Void> createProject(@Validated @RequestBody ProjectDTO projectDTO) {
        projectService.createProject(projectDTO);
        return Result.success();
    }

    @PutMapping
    @ApiOperation("更新项目")
    @PreAuthorize("hasAuthority('admin')")
    public Result<Void> updateProject(@Validated @RequestBody ProjectDTO projectDTO) {
        projectService.updateProject(projectDTO);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除项目")
    @PreAuthorize("hasAuthority('admin')")
    public Result<Void> deleteProject(@PathVariable Long id) {
        projectService.deleteProject(id);
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("获取项目详情")
    public Result<ProjectVO> getProjectDetail(@PathVariable Long id) {
        ProjectVO projectVO = projectService.getProjectDetail(id);
        return Result.success(projectVO);
    }

    @PutMapping("/archive/{id}")
    @ApiOperation("归档项目")
    @PreAuthorize("hasAuthority('admin')")
    public Result<Void> archiveProject(@PathVariable Long id) {
        projectService.archiveProject(id);
        return Result.success();
    }


    @PostMapping("/join")
    @ApiOperation("加入项目")
    public Result<Void> joinProject(@RequestBody String joinCode) {
        projectService.joinProjectByCode(joinCode);
        return Result.success();
    }
}